package spotGUI.mainFrameGUI;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionListener;

import managers.messengerManager.MessengerManager;
import spotGUI.utils.EmptyContentAreaButton;
import spotGUI.utils.MessengerUserSelectionListener;
import spotGUI.utils.UserDataListCellRenderer;
import utils.MaxLenghtTextDocument;
import utils.UserData;
import java.util.ResourceBundle;

public class MessengerMainPanel extends JPanel {

	private static final int FIXED_CELL_WIDTH = 200;

	public static final int CHILDREN = 0;
	public static final int EDUCATORS = 1;
	public static final int ANIMATORS = 2;
	public static final int COOPERATORS = 3;
	public static final int PRIESTS = 4;
	private JTextField textFieldSearch;
	private JLabel lblSearch;
	private JButton btnIscritti;
	private JButton btnAnimatori;
	private JButton btnEducatori;
	private JButton btnDon;
	private Box box;
	private JButton btnCollaboratori;
	private JList listIscritti;
	private JList listAnimatori;
	private JList listEducatori;
	private JList listDon;
	private JList listCollaboratori;
	private JPanel panel;
	private JScrollPane scrollPane;

	private ListSelectionListener userSelectionListener;
	private Component horizontalStrut;
	private Component glue;
	private JLabel lblSearchIcon;



	public MessengerMainPanel() {
		setBorder(new EmptyBorder(10, 10, 10, 10));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{38, 21, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);

		userSelectionListener = new MessengerUserSelectionListener();

		box = Box.createHorizontalBox();
		GridBagConstraints gbc_box = new GridBagConstraints();
		gbc_box.anchor = GridBagConstraints.WEST;
		gbc_box.insets = new Insets(0, 0, 5, 0);
		gbc_box.gridx = 0;
		gbc_box.gridy = 0;
		add(box, gbc_box);

		lblSearchIcon = new JLabel("");
		lblSearchIcon.setIcon(new ImageIcon(MessengerMainPanel.class.getResource("/images/Icons/32x32px/search.png")));
		box.add(lblSearchIcon);

		lblSearch = new JLabel(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("MessengerMainPanel.lblSearch.text")); //$NON-NLS-1$ //$NON-NLS-2$
		box.add(lblSearch);

		textFieldSearch = new JTextField();
		textFieldSearch.setDocument(new MaxLenghtTextDocument(30));
		textFieldSearch.setBackground(UIManager.getColor("Desktop.background"));
		textFieldSearch.setMaximumSize(new Dimension(2147483647, 20));
		textFieldSearch.setMinimumSize(new Dimension(600, 20));
		textFieldSearch.setPreferredSize(new Dimension(500, 20));
		textFieldSearch.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				highlighMatchingCells(textFieldSearch.getText(), listIscritti);
				highlighMatchingCells(textFieldSearch.getText(), listAnimatori);
				highlighMatchingCells(textFieldSearch.getText(), listCollaboratori);
				highlighMatchingCells(textFieldSearch.getText(), listDon);
				repaint();
			}
		});

		horizontalStrut = Box.createHorizontalStrut(20);
		box.add(horizontalStrut);
		box.add(textFieldSearch);
		textFieldSearch.setColumns(10);

		scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBorder(null);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		add(scrollPane, gbc_scrollPane);

		panel = new JPanel();
		scrollPane.setViewportView(panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{451, 0};
		gbl_panel.rowHeights = new int[]{40, 6, 40, 6, 40, 6, 40, 6, 40, 6, 0, 0};
		gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 100.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);

		btnIscritti = new EmptyContentAreaButton((MessengerMainPanel.class.getResource("/images/Icons/24x24px/add.png")),
				ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("MessengerMainPanel.1"));
		btnIscritti.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		GridBagConstraints gbc_btnIscritti = new GridBagConstraints();
		gbc_btnIscritti.anchor = GridBagConstraints.WEST;
		gbc_btnIscritti.insets = new Insets(0, 0, 5, 0);
		gbc_btnIscritti.gridx = 0;
		gbc_btnIscritti.gridy = 0;
		panel.add(btnIscritti, gbc_btnIscritti);

		listIscritti = new JList();
		listIscritti.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		listIscritti.setOpaque(false);
		listIscritti.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listIscritti.addListSelectionListener(userSelectionListener);
		listIscritti.setFixedCellWidth(FIXED_CELL_WIDTH);
		GridBagConstraints gbc_listIscritti = new GridBagConstraints();
		gbc_listIscritti.fill = GridBagConstraints.BOTH;
		gbc_listIscritti.insets = new Insets(0, 0, 5, 0);
		gbc_listIscritti.gridx = 0;
		gbc_listIscritti.gridy = 1;
		panel.add(listIscritti, gbc_listIscritti);
		listIscritti.setModel(new DefaultListModel());
		listIscritti.setLayoutOrientation(JList.VERTICAL_WRAP);
		listIscritti.setCellRenderer(new UserDataListCellRenderer());
		btnIscritti.addActionListener(new ShowListActionListener(listIscritti));

		btnAnimatori = new EmptyContentAreaButton((MessengerMainPanel.class.getResource("/images/Icons/24x24px/add.png")),
				ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("MessengerMainPanel.2"));
		btnAnimatori.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		GridBagConstraints gbc_btnAnimatori = new GridBagConstraints();
		gbc_btnAnimatori.anchor = GridBagConstraints.WEST;
		gbc_btnAnimatori.insets = new Insets(0, 0, 5, 0);
		gbc_btnAnimatori.gridx = 0;
		gbc_btnAnimatori.gridy = 2;
		panel.add(btnAnimatori, gbc_btnAnimatori);

		listAnimatori = new JList();
		listAnimatori.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		listAnimatori.setOpaque(false);
		listAnimatori.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listAnimatori.addListSelectionListener(userSelectionListener);
		listAnimatori.setFixedCellWidth(FIXED_CELL_WIDTH);
		GridBagConstraints gbc_listAnimatori = new GridBagConstraints();
		gbc_listAnimatori.fill = GridBagConstraints.BOTH;
		gbc_listAnimatori.insets = new Insets(0, 0, 5, 0);
		gbc_listAnimatori.gridx = 0;
		gbc_listAnimatori.gridy = 3;
		panel.add(listAnimatori, gbc_listAnimatori);
		listAnimatori.setModel(new DefaultListModel());
		listAnimatori.setLayoutOrientation(JList.VERTICAL_WRAP);
		listAnimatori.setCellRenderer(new UserDataListCellRenderer());
		btnAnimatori.addActionListener(new ShowListActionListener(listAnimatori));

		btnEducatori = new EmptyContentAreaButton((MessengerMainPanel.class.getResource("/images/Icons/24x24px/add.png")),
				ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("MessengerMainPanel.3"));
		btnEducatori.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		GridBagConstraints gbc_btnEducatori = new GridBagConstraints();
		gbc_btnEducatori.anchor = GridBagConstraints.WEST;
		gbc_btnEducatori.insets = new Insets(0, 0, 5, 0);
		gbc_btnEducatori.gridx = 0;
		gbc_btnEducatori.gridy = 4;
		panel.add(btnEducatori, gbc_btnEducatori);

		listEducatori = new JList();
		listEducatori.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		listEducatori.setOpaque(false);
		listEducatori.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listEducatori.addListSelectionListener(userSelectionListener);
		listEducatori.setFixedCellWidth(FIXED_CELL_WIDTH);
		GridBagConstraints gbc_listEducatori = new GridBagConstraints();
		gbc_listEducatori.fill = GridBagConstraints.BOTH;
		gbc_listEducatori.insets = new Insets(0, 0, 5, 0);
		gbc_listEducatori.gridx = 0;
		gbc_listEducatori.gridy = 5;
		panel.add(listEducatori, gbc_listEducatori);
		listEducatori.setModel(new DefaultListModel());
		listEducatori.setLayoutOrientation(JList.VERTICAL_WRAP);
		listEducatori.setCellRenderer(new UserDataListCellRenderer());
		btnEducatori.addActionListener(new ShowListActionListener(listEducatori));

		btnDon = new EmptyContentAreaButton((MessengerMainPanel.class.getResource("/images/Icons/24x24px/add.png")),
				ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("MessengerMainPanel.4"));
		btnDon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		GridBagConstraints gbc_btnDon = new GridBagConstraints();
		gbc_btnDon.anchor = GridBagConstraints.WEST;
		gbc_btnDon.insets = new Insets(0, 0, 5, 0);
		gbc_btnDon.gridx = 0;
		gbc_btnDon.gridy = 6;
		panel.add(btnDon, gbc_btnDon);

		listDon = new JList();
		listDon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		listDon.setOpaque(false);
		listDon.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listDon.addListSelectionListener(userSelectionListener);
		listDon.setFixedCellWidth(FIXED_CELL_WIDTH);
		GridBagConstraints gbc_listDon = new GridBagConstraints();
		gbc_listDon.fill = GridBagConstraints.BOTH;
		gbc_listDon.insets = new Insets(0, 0, 5, 0);
		gbc_listDon.gridx = 0;
		gbc_listDon.gridy = 7;
		panel.add(listDon, gbc_listDon);
		listDon.setModel(new DefaultListModel());
		listDon.setLayoutOrientation(JList.VERTICAL_WRAP);
		listDon.setCellRenderer(new UserDataListCellRenderer());
		btnDon.addActionListener(new ShowListActionListener(listDon));

		btnCollaboratori = new EmptyContentAreaButton((MessengerMainPanel.class.getResource("/images/Icons/24x24px/add.png")),
				ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("MessengerMainPanel.5"));
		btnCollaboratori.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		GridBagConstraints gbc_btnCollaboratori = new GridBagConstraints();
		gbc_btnCollaboratori.anchor = GridBagConstraints.WEST;
		gbc_btnCollaboratori.insets = new Insets(0, 0, 5, 0);
		gbc_btnCollaboratori.gridx = 0;
		gbc_btnCollaboratori.gridy = 8;
		panel.add(btnCollaboratori, gbc_btnCollaboratori);

		listCollaboratori = new JList();
		listCollaboratori.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		listCollaboratori.setOpaque(false);
		listCollaboratori.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listCollaboratori.addListSelectionListener(userSelectionListener);
		listCollaboratori.setFixedCellWidth(FIXED_CELL_WIDTH);
		GridBagConstraints gbc_listCollaboratori = new GridBagConstraints();
		gbc_listCollaboratori.insets = new Insets(0, 0, 5, 0);
		gbc_listCollaboratori.fill = GridBagConstraints.BOTH;
		gbc_listCollaboratori.gridx = 0;
		gbc_listCollaboratori.gridy = 9;
		panel.add(listCollaboratori, gbc_listCollaboratori);
		listCollaboratori.setModel(new DefaultListModel());
		listCollaboratori.setLayoutOrientation(JList.VERTICAL_WRAP);
		listCollaboratori.setCellRenderer(new UserDataListCellRenderer());
		btnCollaboratori.addActionListener(new ShowListActionListener(listCollaboratori));

		glue = Box.createGlue();
		GridBagConstraints gbc_glue = new GridBagConstraints();
		gbc_glue.gridx = 0;
		gbc_glue.gridy = 10;
		panel.add(glue, gbc_glue);

		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent arg0) {
				updateListLayout(listIscritti);
				updateListLayout(listAnimatori);
				updateListLayout(listEducatori);
				updateListLayout(listCollaboratori);
				updateListLayout(listDon);
			}
		});
	}


	public void fillSingleListCell (UserData user) {
		if ((Integer) user.getProperty("id") == MessengerManager.getSharedInstance().getUserId())
			return;
		switch ((Integer) user.getProperty("userType")) {
		case CHILDREN:
			((DefaultListModel)listIscritti.getModel()).addElement(user);
			break;
		case EDUCATORS:
			((DefaultListModel)listEducatori.getModel()).addElement(user);
			break;
		case ANIMATORS:
			((DefaultListModel)listAnimatori.getModel()).addElement(user);
			break;
		case COOPERATORS:
			((DefaultListModel)listCollaboratori.getModel()).addElement(user);
			break;
		case PRIESTS:
			((DefaultListModel)listDon.getModel()).addElement(user);
			break;
		default:
			break;
		}
	}
	
	private void updateListLayout (JList list) {
		int columnCount = scrollPane.getWidth() / FIXED_CELL_WIDTH;
		int rowCount = list.getModel().getSize()/columnCount;
		if (list.getModel().getSize()%columnCount != 0) {
			rowCount++;
		}
		list.setVisibleRowCount(rowCount);
	}

	private void highlighMatchingCells (String key, JList list) {
		ListModel model = list.getModel();
		if (key.length() <= 1) {
			for (int i = 0; i < model.getSize(); i++) {
				((UserData)model.getElementAt(i)).putProperty("matchingWithSearchKey", false);
			}
		} else {
			String lowerCaseKey = key.toLowerCase();
			for (int i = 0; i < model.getSize(); i++) {
				UserData temp = ((UserData)model.getElementAt(i));
				if ((((String) temp.getProperty("firstName")).toLowerCase().startsWith(lowerCaseKey) || ((String) temp.getProperty("surname")).toLowerCase().startsWith(lowerCaseKey) ||
						((((String) temp.getProperty("firstName")).toLowerCase()+" "+(String) temp.getProperty("surname")).toLowerCase().startsWith(lowerCaseKey)))) {
					temp.putProperty("matchingWithSearchKey", true);
					System.out.println(temp.getProperty("FirstName"));
				} else {
					temp.putProperty("matchingWithSearchKey", false);
				}
			}
		}
	}
	
	public void clearSelections () {
		listIscritti.clearSelection();
	}
	

	private class ShowListActionListener implements ActionListener {

		private JList list;

		public ShowListActionListener (JList list) {
			this.list = list;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if (list.isVisible())
				list.setVisible(false);
			else
				list.setVisible(true);
		}

	}
}



